/**
 * 订阅model
 */
'use strict'
import {district} from 'antd-mobile-demo-data';
import {filterClass} from "../services/bookService";
import {query} from "../services/homeService";

const sbkc = [
  {name: '少儿英语', value: 'sryy'},
  {name: '成人英语', value: 'cryy'},
  {name: '商务英语', value: 'swyy'},
  {name: '雅思/托福', value: 'ystf'},
  {name: '韩语', value: 'hy'},
  {name: '日语', value: 'ry'},
  {name: '菲语', value: 'fy'},
];
const skfs = [
  {name: '老师上门', value: 'lssm'},
  {name: '学生上门', value: 'xssm'},
];

const lsxb = [
  {name: '男', value: 'nan'},
  {name: '女', value: 'nv'},
];

const sksj = [
  {name: '周一', value: 'zy'},
  {name: '周二', value: 'ze'},
  {name: '周三', value: 'zs'},
  {name: '周四', value: 'zsi'},
  {name: '周五', value: 'zw'},
  {name: '周六', value: 'zl'},
  {name: '周日', value: 'zr'},
  {name: '早上', value: 'zshang'},
  {name: '下午', value: 'xw'},
  {name: '晚上', value: 'ws'},
];

const jxjy = [
  {name: '3年以下', value: '3n'},
  {name: '3-5年', value: '3n-5n'},
  {name: '5年以上', value: '5n'},
];
const bzgm = [
  {name: '一对一', value: '1d1'},
  {name: '3-5人', value: '3-5r'},
  {name: '6-8人', value: '6-8r'},
];
export default {

  namespace: 'book',

  state: {
    filterVisible: false,//筛选条件显示开关
    filterCondition: {
      skfs: {
        name: "skfs",
        value: "lssm",
        touched: true,
      },
      bzgm: {
        name: "bzgm",
        value: "1d1",
        touched: true,
      },
    },//筛选条件
    filterData: {
      sbkc: sbkc,//所补课程
      szqy: district,//所在城市
      skfs: skfs,
      lsxb: lsxb,
      sksj: sksj,
      jxjy: jxjy,
      bzgm: bzgm,
    },//待选择的条件
    bookClasses: [],//预订班级
    pagination: {},//分页
  },

  subscriptions: {
    setup({dispatch, history}) {  // eslint-disable-line
    },
  },

  effects: {
    * search({val}, {call, put}) {  // eslint-disable-line
      const res = yield call(_ => query(val));
      yield put({type: 'save', res});
    },
    * bookFilter({body}, {call, put, select}) {
      // const {filterCondition} = yield select(stores => stores.book);
      // const body = {filterCondition};
      let res = yield call(_ => filterClass(body)) || {};

      if (res == null)  res = {};
      let {data = {}} = res;
      if (data == null)  data = {};

      const {
        data: bookClasses = [],
        page = 0,
        pageSize = 0,
        total = 0,
        totalPage = 0
      } = data;
      const pagination = {page, pageSize, total, totalPage};
      yield put({type: 'bookFilterSet', bookClasses, pagination});
    },
  },

  reducers: {
    filterVisible(state = {}, {filterVisible = false}) {
      return {...state, filterVisible: !state.filterVisible};
    },
    filterCondition(state = {}, {payload = {}}) {
      return {...state, filterCondition: payload};
    },
    bookFilterSet(state = {}, {bookClasses = [], pagination}) {
      return {...state, bookClasses, pagination, filterVisible: false};
    },
    clear(state = {}, {payload = {}}) {
      return {
        filterVisible: false,//筛选条件显示开关
        filterCondition: {},//筛选条件
        filterData: {
          sbkc: sbkc,//所补课程
          szqy: district,//所在城市
          skfs: skfs,
          lsxb: lsxb,
          sksj: sksj,
          jxjy: jxjy,
          bzgm: bzgm,
        },//待选择的条件
      }
    },
  },

};
